import { defineStore } from 'pinia'
import { literatureApi, type Literature, type LiteratureQuery, type PageResult } from '@/api/literature'

export const useLiteratureStore = defineStore('literature', {
  state: () => ({
    literatureList: [] as Literature[],
    currentLiterature: null as Literature | null,
    favoriteList: [] as Literature[],
    pagination: {
      current: 1,
      size: 10,
      total: 0,
      pages: 0
    },
    loading: false,
    query: {
      current: 1,
      size: 10
    } as LiteratureQuery
  }),

  actions: {
    // 获取文献列表
    async fetchLiteratureList(query?: Partial<LiteratureQuery>) {
      this.loading = true
      try {
        const searchQuery = { ...this.query, ...query }
        const result: PageResult<Literature> = await literatureApi.getList(searchQuery)
        
        this.literatureList = result.records
        this.pagination = {
          current: result.current,
          size: result.size,
          total: result.total,
          pages: result.pages
        }
        this.query = searchQuery
      } catch (error) {
        console.error('获取文献列表失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 获取文献详情
    async fetchLiteratureById(id: number) {
      try {
        const literature = await literatureApi.getById(id)
        this.currentLiterature = literature
        return literature
      } catch (error) {
        console.error('获取文献详情失败:', error)
        throw error
      }
    },

    // 上传文献
    async uploadLiterature(formData: FormData) {
      try {
        const id = await literatureApi.upload(formData)
        // 上传成功后刷新列表
        await this.fetchLiteratureList()
        return id
      } catch (error) {
        console.error('上传文献失败:', error)
        throw error
      }
    },

    // 更新文献
    async updateLiterature(id: number, data: Partial<Literature>) {
      try {
        await literatureApi.update(id, data)
        // 更新成功后刷新列表
        await this.fetchLiteratureList()
        if (this.currentLiterature?.id === id) {
          await this.fetchLiteratureById(id)
        }
      } catch (error) {
        console.error('更新文献失败:', error)
        throw error
      }
    },

    // 删除文献
    async deleteLiterature(id: number) {
      try {
        await literatureApi.delete(id)
        // 删除成功后刷新列表
        await this.fetchLiteratureList()
      } catch (error) {
        console.error('删除文献失败:', error)
        throw error
      }
    },

    // 下载文献
    async downloadLiterature(id: number) {
      try {
        await literatureApi.download(id)
      } catch (error) {
        console.error('下载文献失败:', error)
        throw error
      }
    },

    // 收藏/取消收藏
    async toggleFavorite(id: number) {
      try {
        await literatureApi.toggleFavorite(id)
        // 切换收藏状态后更新相关数据
        if (this.currentLiterature?.id === id) {
          this.currentLiterature.isFavorite = !this.currentLiterature.isFavorite
        }
        
        // 更新列表中的收藏状态
        const literature = this.literatureList.find(item => item.id === id)
        if (literature) {
          literature.isFavorite = !literature.isFavorite
        }
      } catch (error) {
        console.error('收藏操作失败:', error)
        throw error
      }
    },

    // 获取收藏列表
    async fetchFavoriteList(query?: Partial<LiteratureQuery>) {
      this.loading = true
      try {
        const searchQuery = { ...this.query, ...query }
        const result: PageResult<Literature> = await literatureApi.getFavorites(searchQuery)
        
        this.favoriteList = result.records
        this.pagination = {
          current: result.current,
          size: result.size,
          total: result.total,
          pages: result.pages
        }
      } catch (error) {
        console.error('获取收藏列表失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 设置查询条件
    setQuery(query: Partial<LiteratureQuery>) {
      this.query = { ...this.query, ...query }
    },

    // 重置查询条件
    resetQuery() {
      this.query = {
        current: 1,
        size: 10
      }
    },

    // 分页变化
    handlePageChange(current: number, size?: number) {
      this.query.current = current
      if (size) {
        this.query.size = size
      }
      this.fetchLiteratureList()
    }
  }
})